package org.orbroker.adapt

import java.sql.{ PreparedStatement, CallableStatement }

trait ParameterAdapter {
  /**
   * Set the parameter value at the given index on the prepared statement.
   * @param ps Statement being prepared for execution
   * @param parm The parameter value
   * @param parmIdx The parameter index
   */
  def setParameter(id: Symbol, ps: PreparedStatement, parm: Any, parmIdx: Int)
}

/**
 * The default parameter adapter.
 */
trait DefaultParameterAdapter extends ParameterAdapter {
  def setParameter(id: Symbol, ps: PreparedStatement, parm: Any, parmIdx: Int) = 
    ps.setObject(parmIdx, parm)
}

/**
 * General adapter for drivers that does not approve
 * of passing `null` to [[java.sql.PreparedStatement#setObject(Int,java.lang.Object)]].
 */
trait NullParmAdapter extends DefaultParameterAdapter { 

  override def setParameter(id: Symbol, ps: PreparedStatement, parm: Any, parmIdx: Int) { 
    if (parm == null) {
      ps.setNull(parmIdx, java.sql.Types.VARCHAR)
    } else {
      super.setParameter(id, ps, parm, parmIdx)
    }
  }

}